package com.caiyouxi.user.controller;

import com.caiyouxi.common.bean.ResData;
import com.caiyouxi.common.control.BaseController;
import com.caiyouxi.common.exception.ErrorCode;
import com.caiyouxi.user.model.PublicAttributeBean;
import com.caiyouxi.user.model.UserProfile;
import com.caiyouxi.user.model.UserProfileBean;
import com.caiyouxi.user.service.UserProfileService;
import com.caiyouxi.user.service.UserService;
import com.caiyouxi.user.util.message.SendSms;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@RestController
@RequestMapping("/user")
public class UserProfileController extends BaseController{

    private Logger logger = Logger.getLogger(UserProfileController.class);

    @Autowired
    private UserProfileService userProfileService;
    @Autowired
    private UserService userService;

    @Value("${sms.path}")
    private  String path ;

    @Value("${sms.username}")
    private  String username ;

    @Value("${sms.password}")
    private  String password ;

    @Value("${sms.signature}")
    private  String signature ;

    @Value("${sms.sendMsg.cmd}")
    private  String cmd ;

    public SendSms test(){
        SendSms sendSms = new SendSms();
        sendSms.setUserName(username);
        sendSms.setPassWord(password);
        sendSms.setPath(path);
        sendSms.setCmd(cmd);
        sendSms.setBody(signature);
        return sendSms;
    }


    /**
     * 获取用户信息
     * @return
     */
    @RequestMapping("/info")
    public ResData get_info(HttpServletRequest request, HttpServletResponse response, @RequestBody UserProfile bean){

        logger.info("====================================  获取用户信息   ==============================");
        if(null == bean.getId()){
            return failed(ErrorCode.SYS_PARAMTER_MISSING.getCode(),ErrorCode.SYS_PARAMTER_MISSING.getMsg(),null);
        }
        return userProfileService.get_info(bean);
    }
    /**
     * 更新用户信息
     * @return
     */
    @RequestMapping("/update")
    public ResData update(HttpServletRequest request, HttpServletResponse response, @RequestBody UserProfile bean){
        logger.info("====================================  更新用户信息   ==============================");
        if(null == bean.getId()){
            return failed(ErrorCode.SYS_PARAMTER_MISSING.getCode(),ErrorCode.SYS_PARAMTER_MISSING.getMsg(),null);
        }
        return userProfileService.update(bean);
    }


    /**
    * 自动登录
    */
    @RequestMapping("/auto_login")
    public ResData auto_login(HttpServletRequest request, HttpServletResponse response, @RequestBody PublicAttributeBean publicAttributeBean){
        logger.info("====================================  自动登录   ==============================");
        return  userService.auto_login(publicAttributeBean);
    }

    /**
     * 一键注册
     */
    @RequestMapping("/auto_reg")
    public ResData auto_reg(HttpServletRequest request, HttpServletResponse response, @RequestBody PublicAttributeBean publicAttributeBean){

        logger.info("====================================  一键注册   ==============================");
        return  userService.auto_reg(publicAttributeBean);
    }


    /**
    * 绑定换绑定手机
    */
    @RequestMapping("/bind_phone")
    public ResData bind_phone(HttpServletRequest request, HttpServletResponse response, @RequestBody PublicAttributeBean publicAttributeBean){

        logger.info("====================================  绑定换绑定手机   ==============================");
        return  userService.bind_phone(publicAttributeBean);
    }


    /**
     * 修改昵称
     */
    @RequestMapping("/change_nickname")
    public ResData change_nickname(HttpServletRequest request, HttpServletResponse response, @RequestBody PublicAttributeBean publicAttributeBean){

        logger.info("====================================  修改昵称   ==============================");
        return  userService.change_nickname(publicAttributeBean);
    }

    /**
     * 修改密码
     */
    @RequestMapping("/change_password")
    public ResData change_password(HttpServletRequest request, HttpServletResponse response, @RequestBody PublicAttributeBean publicAttributeBean){
        logger.info("====================================  修改密码   ==============================");
        return  userService.change_password(publicAttributeBean);
    }

    /**
    * 邮箱验证登录
    */
    @RequestMapping("/email_login")
    public ResData email_login(HttpServletRequest request, HttpServletResponse response, @RequestBody PublicAttributeBean publicAttributeBean){

        logger.info("====================================  邮箱验证登录   ==============================");
        return  userService.email_login(publicAttributeBean);
    }

    /**
     * 用户邮箱注册
     */
    @RequestMapping("/email_reg")
    public ResData email_reg(HttpServletRequest request, HttpServletResponse response, @RequestBody PublicAttributeBean publicAttributeBean){

        logger.info("====================================  用户邮箱注册   ==============================");
        return userService.email_reg(publicAttributeBean);
    }
    /**
     * 根据TOKEN查询用户
     */
    @RequestMapping("/get_by_token")
    public ResData get_by_token(HttpServletRequest request, HttpServletResponse response, @RequestBody PublicAttributeBean publicAttributeBean){
        logger.info("====================================  根据TOKEN查询用户   ==============================");
        return userService.get_by_token(publicAttributeBean);
    }
    /**
     * 用户名密码登录
    */
    @RequestMapping("/login")
    public ResData login(HttpServletRequest request, HttpServletResponse response, @RequestBody PublicAttributeBean publicAttributeBean){

        logger.info("====================================  用户名密码登录   ==============================");
        return userService.login(publicAttributeBean);
    }
    /**
     * 手机短信验证码登录
     */
    @RequestMapping("/phone_login")
    public ResData phone_login(HttpServletRequest request, HttpServletResponse response, @RequestBody PublicAttributeBean publicAttributeBean){
        logger.info("====================================  手机短信验证码登录   ==============================");
     return  userService.phone_login(publicAttributeBean);
    }
    /**
     * 用户手机号码注册
     */
    @RequestMapping("/phone_reg")
    public ResData phone_reg(HttpServletRequest request, HttpServletResponse response, @RequestBody PublicAttributeBean publicAttributeBean) {

        logger.info("====================================  用户手机号码注册   ==============================");
        return userService.phone_reg(publicAttributeBean);
    }
    /**
    * 用户名注册
    */
    @RequestMapping("/reg")
    public ResData reg(HttpServletRequest request, HttpServletResponse response, @RequestBody PublicAttributeBean publicAttributeBean){
        logger.info("====================================  用户名注册   ==============================");
        return userService.reg(publicAttributeBean);
    }
    /**
     * 重置密码
     */
    @RequestMapping("/reset_password")
    public ResData reset_password(HttpServletRequest request, HttpServletResponse response, @RequestBody UserProfileBean userProfileBean){
        logger.info("====================================  重置密码   ==============================");

        SendSms sendSms=test();
        return userService.reset_password(userProfileBean,sendSms);
    }
    /**
     * 重置修改密码
     */
    @RequestMapping("/reset_pswd")
    public ResData reset_pswd(HttpServletRequest request, HttpServletResponse response, @RequestBody UserProfileBean userProfileBean){

        logger.info("====================================  重置修改密码   ==============================");
        SendSms sendSms=test();
        return userService.reset_pswd(userProfileBean,sendSms);
    }
    /**
     * 发送手机验证码
     */
      @RequestMapping("/send_phone_code")
      public ResData send_phone_code(HttpServletRequest request, HttpServletResponse response, @RequestBody PublicAttributeBean publicAttributeBean){

          logger.info("====================================  发送手机验证码   ==============================");
          SendSms sendSms=test();
          return userService.send_phone_code(publicAttributeBean,sendSms);
      }
    /**
     * 验证手机验证码
     */
    @RequestMapping("/validate_phone_code")
    public ResData validate_phone_code(HttpServletRequest request, HttpServletResponse response, @RequestBody PublicAttributeBean publicAttributeBean){

        logger.info("====================================  验证手机验证码   ==============================");
      return userService.validate_phone_code(publicAttributeBean);
    }
    /**
     * 实名认证
     */
    @RequestMapping("/validate_realname")
    public ResData validate_realname(HttpServletRequest request, HttpServletResponse response, @RequestBody PublicAttributeBean publicAttributeBean){
        logger.info("====================================  实名认证   ==============================");
        return userService.validate_realname(publicAttributeBean);
    }
    /**
     * 验证对应的用户是否存在
     */
    @RequestMapping("/verify_exist")
    public ResData verify_exist(HttpServletRequest request, HttpServletResponse response, @RequestBody PublicAttributeBean publicAttributeBean) {
        logger.info("====================================  验证对应的用户是否存在   ==============================");
        return userService.verify_exist(publicAttributeBean);
    }

    /**
     * 验证对应的用户是否存在
     */
    @RequestMapping("/realname_exist")
    public ResData realname_exist(HttpServletRequest request, HttpServletResponse response, @RequestBody PublicAttributeBean publicAttributeBean){

        logger.info("====================================  验证对应的用户是否存在   ==============================");
        return  userService.realname_exist(publicAttributeBean);
    }




}
